/**
 * Created by Khacheresyants.L on 2/5/14.
 */
function getScrollMaxY() {

    var innerh;

    if (window.innerHeight) {
        innerh = window.innerHeight;
    } else {
        innerh = document.body.clientHeight;
    }

    if (window.innerHeight && window.scrollMaxY) {
        // Firefox
        yWithScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
    } else {
        // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        yWithScroll = document.body.offsetHeight;
    }
    return yWithScroll - innerh;
}

window.isLoading = false;
window.isAll = false;

$(document).ready(function () {
    var o = {
        textFont: 'Arial, Helvetica, sans-serif', maxSpeed: 0.05, minSpeed: 0.002,
        textColour: '#369', textHeight: 25, outlineMethod: 'outline',
        outlineThickness: 5, outlineColour: '#f93', outlineOffset: 5, depth: 0.75,
        minBrightness: 0.2, wheelZoom: true, reverse: true, shadowBlur: 2,
        shuffleTags: true, shadowOffset: [0, 1], initial: [0, 0.01],
        splitWidth: 180, txtOpt: true
    };
    try {
        TagCanvas.Start('tag-canvas', 'tags', o);
//            {
    } catch (e) {
        $('#tag-field').hide();
    }
    var content = $('#content');
    content.imagesLoaded(function () {
        content.masonry({
//            "columnWidth": 142.5,
            itemSelector: '.item'
//            isAnimated: 'true',
//            isFitWidth: 'true'
        });
    });

    $(window).scroll(function () {
        var curPos = $(window).scrollTop();
        var maxY = getScrollMaxY();

        if (curPos > (maxY - 300) && window.isLoading == false && window.isAll == false) {
            window.isLoading = true;
            $.ajax({
                type: 'get',
                traditional: true,
                url: url + $('div#content > div').length,
                success: function (html) {
                    var $products = $(html).css({opacity: 0});
                    var $count = $products.filter('div.item').length;
                    content.append($products);
                    imagesLoaded(content, function () {
                        content.masonry('appended', $products);
                    });


                    if ($count < 25) {
                        window.isAll = true;
                    }
                    window.isLoading = false;

                },
                error: function (data) {
                    window.isLoading = false;
                }
            });

        }
    });
    $(function () {

        $(window).scroll(function () {

            if ($(this).scrollTop() != 0) {

                $('#toTop').fadeIn();

            } else {

                $('#toTop').fadeOut();

            }

        });

        $('#toTop').click(function () {

            $('body,html').animate({scrollTop: 0}, 800);

        });

    });
});

